//
//  LQWebProgressView.m
//  LingQianZhu
//
//  Created by 谢英泽 on 2016/12/21.
//  Copyright © 2016年 谢英泽. All rights reserved.
//

#import "ZCWebProgressView.h"
#import "AppHeader.h"

@interface ZCWebProgressView ()

@property (nonatomic, strong) CAShapeLayer *progressLayer;

@end

@implementation ZCWebProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(kUI_WIDTH, 0)];
        
        //遮挡层
        self.progressLayer = [CAShapeLayer layer];
        self.progressLayer.frame = self.bounds;
        self.progressLayer.fillColor = kCOLOR_clear.CGColor;
        self.progressLayer.strokeColor = self.strokeColor.CGColor;
        self.progressLayer.lineCap = kCALineCapSquare;
        self.progressLayer.lineWidth = CGRectGetHeight(self.progressLayer.frame);
        self.progressLayer.path = path.CGPath;
        
        //渐变色
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = self.bounds;
        gradientLayer.colors = @[(__bridge id)kCOLOR_white.CGColor,(__bridge id)kCOLOR_lightGray.CGColor];
        gradientLayer.locations = @[@0, @1];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint   = CGPointMake(1, 0);
        gradientLayer.mask = self.progressLayer;
        [self.layer addSublayer:gradientLayer];
    }
    return self;
}

- (void)startAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 5;
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue = [NSNumber numberWithFloat:0.6];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.delegate = self;
    [self.progressLayer addAnimation:animation forKey:@"one"];
}

- (void)finishAnimation
{
    CGFloat presentValue = ((CAShapeLayer *)self.progressLayer.presentationLayer).strokeEnd;
    [self.progressLayer removeAllAnimations];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 0.35;
    animation.fromValue = [NSNumber numberWithFloat:presentValue];
    animation.toValue = [NSNumber numberWithFloat:1];
    [self.progressLayer addAnimation:animation forKey:@"finish"];
}

- (void)stopAnimation
{
    [self.progressLayer removeAllAnimations];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //动画完成还未加载完网页，延时效果
    if ([self.progressLayer animationForKey:@"one"] == anim) {
        if (flag) {
            CGFloat presentValue = ((CAShapeLayer *)self.progressLayer.presentationLayer).strokeEnd;
            
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
            animation.duration = 5;
            animation.fromValue = [NSNumber numberWithFloat:presentValue];
            animation.toValue = [NSNumber numberWithFloat:0.9];
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeForwards;
            [self.progressLayer addAnimation:animation forKey:@"two"];
        }
    }
}

@end
